#include "Dac.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <QDebug>

Dac::Dac(QObject *parent) : QObject (parent)
{
#if __arm__
    system("echo 0 > /sys/devices/platform/soc/40017000.dac/40017000.dac:dac@1/iio:device1/out_voltage1_powerdown");
#endif
}

void Dac::setDac(QString dac)
{
    char const *filename = "/sys/devices/platform/soc/40017000.dac/40017000.dac:dac@1/iio:device1/out_voltage1_raw";
    //char const *filename = "/sys/devices/platform/soc/40017000.dac/40017000.dac:dac@1/iio:device1/outx";
    int err = 0;
    int fd;
    QByteArray ba;
    ba = dac.toLatin1();
    char *buf = ba.data();

    fd = open(filename, O_RDWR);
    if(fd < 0){
        close(fd);
        qDebug()<<"open file error!"<<endl;
        return;
    }

    err = write(fd, buf, sizeof(buf));
    if (err < 0){
        close(fd);
        qDebug()<<"read data error!"<<endl;
        return;
    }
    close(fd);
}

Dac::~Dac()
{

}
